<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="tex/html; charset=UTF-8">
<base href="<%=basePath%>">
<title>模块分配</title>
<link rel="stylesheet" href="res/zTree_v3/css/demo.css" type="text/css">
	<link rel="stylesheet" href="res/zTree_v3/css/zTreeStyle/zTreeStyle.css" type="text/css">
	<script type="text/javascript" src="res/layui/jquery-1.9.1.min.js"></script>
	<script type="text/javascript" src="res/zTree_v3/js/jquery.ztree.core.js"></script>
	<script type="text/javascript" src="res/zTree_v3/js/jquery.ztree.excheck.js"></script>
	<script src="res/layui/layui.js"></script>
    <link rel="stylesheet" href="res/layui/css/layui.css"></head>
	
	<SCRIPT type="text/javascript">
		
		var zNodes;
		var url = "<%=basePath%>/model/ztreemodel.action";
		$.ajax({
			type:'post',
			url:url,
			async:false,
			dataTye:'json',
			success:function(info){
				
				zNodes=info;
			}
		});
		
		$(function(){
			var id = GetQueryString("id");
			$("#roleId").val(id);
		})	
		
		var setting = {
			check: {
				enable: true,
				chkDisabledInherit: true
			},
			data: {
				simpleData: {
					enable: true
				}
			}
		};

		
		function disabledNode(e) {
			var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
			disabled = e.data.disabled,
			nodes = zTree.getSelectedNodes(),
			
			inheritParent = false, inheritChildren = false;
			if (nodes.length == 0) {
				alert("请先选择一个节点");
			}
			if (disabled) {
				inheritParent = $("#py").attr("checked");
				inheritChildren = $("#sy").attr("checked");
			} else {
				inheritParent = $("#pn").attr("checked");
				inheritChildren = $("#sn").attr("checked");
			}

			for (var i=0, l=nodes.length; i<l; i++) {
				zTree.setChkDisabled(nodes[i], disabled, inheritParent, inheritChildren);
			}
		}

		$(document).ready(function(){
			$.fn.zTree.init($("#treeDemo"), setting, zNodes);
			$("#disabledTrue").bind("click", {disabled: true}, disabledNode);
			$("#disabledFalse").bind("click", {disabled: false}, disabledNode);
			
		});
			
			//立即提交
			function superior(){
				var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
				var nodes = treeObj.getCheckedNodes(true);
				var moduleid = "";
				for(var i=0;i<nodes.length;i++){
					if(i==nodes.length-1){
						moduleid+=nodes[i].id;
					}else{
						moduleid+=nodes[i].id+"_";
					}
				}
				var roleId = $("#roleId").val();
				var data={"moduleid":moduleid,"roleId":roleId};
				var url = "<%=basePath%>/rolemodel/saverolemodelTwo.action";
				$.ajax({
					type:'post',
					url:url,
					data:data,
					dataTye:'json',
					success:function(info){
						if(info.state=="1"){
							parent.layer.msg(info.mes);
							var int=self.setInterval(function(){  // 这个方法是说在延迟两秒后执行大括号里的方法
			           		 	parent.location.reload();
			           	 		 // 这个方法是刷新当前页面
			           	    },1000)
						}else{
							
						}
					}
				});
			}
			
			function GetQueryString(id){
			     var reg = new RegExp("(^|&)"+ id +"=([^&]*)(&|$)");
			     var r = window.location.search.substr(1).match(reg);
			     if(r!=null)return  unescape(r[2]); return null;
			}
		</SCRIPT>
	</HEAD>

	<BODY>
	
<div class="content_wrap">
	<div class="zTreeDemoBackground left" style="margin: 20px;position: relative;left: 20%;">
	<input type="hidden" value="" id="roleId" name="roleId"/>
<div class="demoTable">
	<button class="layui-btn layui-btn-mini" lay-submit lay-filter="formDemo" onclick="superior()">立即提交</button>
	 <div class="zTreeDemoBackground left">
		<ul id="treeDemo" class="ztree"></ul>
	</div>
</div>
			
			
	</div>
</div>		
<div id="menuContent" class="menuContent" style="display:none; position: absolute;">
	<ul id="treeDemo" class="ztree" style="margin-top:0; width:180px; height: 300px;"></ul>
</div>
	</BODY>
	</HTML>